<?php
/**
 * @datetime 15-1-14
 * AbstractCompress.php
 */
namespace ulue\libs\compress;

use ulue\libs\fileSystem\Directory,
    ulue\libs\helpers\StringHelper;

abstract class AbstractCompress implements InterfaceCompress
{
    public $output;
    public $inputFiles = [];

    public function __construct()
    {

    }

    abstract public function suffix();

    abstract public function execute($files, $return);

    public function save($file)
    {

        if ($this->output == '') {
            \Trigger::error("没有内容可以保存！");
        }

        $info = $this->createName($file);

        $dir      = $info['dir'];

        if (!is_dir($dir)) {
            Directory::create($dir);
        }

        return @file_put_contents($info['path'], $this->output)===false ? false : $info['path'];
    }

    public function checkExtensionLoaded($exts='zlib')
    {
        $exts = StringHelper::toArray($exts);

        if (empty($exts) || !is_array($exts)) {
            throw new \InvalidArgumentException('传入了错误的参数!');
        }

        //检查服务器是否开启了zlib拓展
        foreach ($exts as $ext) {
            if( !extension_loaded(trim($ext)) ){
                throw new \DNotFoundException("类运行必须的扩展 {$ext} 没有加载或开启！");
            }
        }
    }

    /**
     * [checkFileExists 检查文件是否存在]
     * @param  array $files 要检查的文件(文件列表)
     * @param  string $ext
     * @throws \DMissArgumentException
     * @throws \DNotFoundException
     * @return array
     */
    public function checkFileExists($files, $ext='')
    {
        $ext    = $ext ? '.'.trim($ext,'. ') : false;
        $files  = StringHelper::toArray($files);

        foreach ($files as $file)
        {
            $file = trim($file);

            if (!file_exists($file)) {
                throw new \DNotFoundException("文件 {$file} 不存在！");
            }

            if ( $ext && strrchr($file,'.') != $ext ) {
                throw new \DMissArgumentException("{$file} 不是 ".trim($ext,'.')." 文件！");
            }
        }

        return $files;
    }

    public function handleStringToArray($string, $separator=',')
    {
        if (is_string($string)) {
            $string = trim( str_replace(' ', '', $string), ', ');
            $string = strpos($string, ',')===false ? array($string) : explode(',', $string);
        }

        return $string;
    }

    public function createName($file,$keyword='min')
    {
        if ( strpos($file,'.')=== false ) {
            \Trigger::error("请传入要保存的路径(含文件名)！");
        }

        $filedir  = dirname($file);
        $filename = basename($file);
        $ext      = $this->getSuffix();

        if ( strrchr($filename, '.') != '.'.$ext ) {
            \Trigger::error("请传入相对应的后缀($ext)的文件名！");
        }

        $filename = preg_replace("/\.($ext)$/i", '.'.$keyword.'.'.$ext, $filename,1);

        return ['name'=>$filename, 'dir'=>$filedir, 'path'=>$filedir.'/'.$filename];

    }

    public function getSuffix()
    {
        $suffix = $this->suffix();

        if (($suffix = trim($suffix,'. ')) == '') {
            throw new \DMissArgumentException("请通过 ".__CLASS__."::suffix() 设置后缀名！", 1);
        }

        return $suffix;
    }

    public function clearChar()
    {
        $rules  = $this->pregRules();
        $this->output = preg_replace( $rules['pattern'], $rules['replace'], $this->output);
    }
}